package com.letu.log.appender;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
import com.alibaba.fastjson.JSONObject;
import com.letu.cache.redis.RedisOps;
import com.letu.core.plumelog.BaseLogMessage;
import com.letu.core.plumelog.CompressMessage;
import com.letu.core.point.AllLogBasePoint;
import com.letu.core.point.LogBasePoint;
import com.letu.core.utils.HeaderUtil;
import com.letu.core.utils.SpringUtils;
import com.letu.log.point.AllLogPointEvent;
import com.letu.plumelog.util.LZ4Util;
import com.letu.plumelog.util.LogMessageUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * className：RedisAppender
 * description：
 * time：2020-05-19.15:26
 *
 * @author Tank
 * @version 1.0.0
 */
public class RedisAppender extends AppenderBase<ILoggingEvent> {
    private String appName;
    private String env = "default";
    private int port;

    private String model = "standalone";
    private String expand;
    private int maxCount = 100;
    private int logQueueSize = 10000;
    private int threadPoolSize = 1;
    private boolean compressor = false;

    public void setPort(int port) {
        this.port = port;
    }

    public int getPort() {
        return port;
    }

    public String getExpand() {
        return expand;
    }

    public void setExpand(String expand) {
        this.expand = expand;
    }

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public String getEnv() {
        return env;
    }

    public void setEnv(String env) {
        this.env = env;
    }

    public int getThreadPoolSize() {
        return threadPoolSize;
    }

    public void setThreadPoolSize(int threadPoolSize) {
        this.threadPoolSize = threadPoolSize;
    }


    public int getMaxCount() {
        return maxCount;
    }

    public void setMaxCount(int maxCount) {
        this.maxCount = maxCount;
    }

    public int getLogQueueSize() {
        return logQueueSize;
    }

    public void setLogQueueSize(int logQueueSize) {
        this.logQueueSize = logQueueSize;
    }

    public boolean isCompressor() {
        return compressor;
    }

    public void setCompressor(boolean compressor) {
        this.compressor = compressor;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    @Override
    protected void append(ILoggingEvent event) {
        if (event != null) {
            send(event);
        }
    }

    protected void send(ILoggingEvent event) {
        BaseLogMessage logMessage = LogMessageUtil.getLogMessage(appName, env, event, port);
//        if (logMessage instanceof RunLogMessage) {
//            message = LogMessageUtil.getLogMessage(logMessage, event);
//        }
        //压缩
//        List<String> list = new ArrayList<>();
//        list.add(JSONObject.toJSONString(logMessage));
//        CompressMessage compressMessage = LZ4Util.compressedMessage(list);
        // TODO: 2022/1/21 发布业务事件
        AllLogBasePoint allLogBasePoint = new AllLogBasePoint();
        allLogBasePoint.setLogMessage(logMessage);
        //发送给applicatoin 事件发布
        SpringUtils.publishEvent(new AllLogPointEvent(allLogBasePoint));
//        System.out.println(JSONObject.toJSONString(event));
//        System.out.println(String.format("message is %s", message));
    }

    @Override
    public void start() {
        super.start();
        System.out.println("开始执行日志打印");
    }
}
